var map;
var selectedMarkers = {};

function initialize() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(definePositionAndLoadMap);
	} else {
		var mapOptions = {
			center: new google.maps.LatLng(-34.397, 150.644),
			zoom: 8,
			disableDefaultUI: true,
			zoomControl: true,
			zoomControlOptions: {
			        style: google.maps.ZoomControlStyle.LARGE,
			        position: google.maps.ControlPosition.LEFT_CENTER
			    },
			mapTypeControl: true,
			scaleControl: true,
			mapTypeId: google.maps.MapTypeId.TERRAIN
		};

		map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

		defineListners();
		// Create the DIV to hold the control and call the HomeControl() constructor
		  // passing in this DIV.
		  var homeControlDiv = document.createElement('div');
		  var homeControl = new GenerateWPTControl(homeControlDiv, map);

		  homeControlDiv.index = 5;
		  map.controls[google.maps.ControlPosition.LEFT_TOP].push(homeControlDiv);
	}
}

function defineListners() {
	google.maps.event.addListener(map, 'click', function(event) {
		placeMarker(event.latLng);
	});
}

function placeMarker(location) {
	var marker = new google.maps.Marker({
		position: location,
		map: map,
		title: 'WPT' + new Date().getTime()
	});
	google.maps.event.addListener(marker, 'dblclick', function(event) {
		removeFromSelected(marker.getTitle());
		marker.setMap(null);
	});
	selectedMarkers[marker.getTitle()] = marker;
	}

	function removeFromSelected(title) {
		delete selectedMarkers[title];
	}

	function definePositionAndLoadMap(position) {
		var mapOptions = {
			center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
			zoom: 6,
			disableDefaultUI: true,
			zoomControl: true,
			zoomControlOptions: {
			        style: google.maps.ZoomControlStyle.LARGE,
			        position: google.maps.ControlPosition.LEFT_CENTER
			    },
			mapTypeControl: true,
			scaleControl: true,
			mapTypeId: google.maps.MapTypeId.TERRAIN
		};

		map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
		defineListners();
		
		var homeControlDiv = document.createElement('div');
		  var homeControl = new GenerateWPTControl(homeControlDiv, map);

		  homeControlDiv.index = 5;
		  map.controls[google.maps.ControlPosition.LEFT_TOP].push(homeControlDiv);
	}
	
function GenerateFile(){
	var line=1;
	var file='';
	file = file + 'OziExplorer Waypoint File Version 1.0\n'
	file = file + 'WGS 84\n'
	file = file + 'Reserved 2\n'
	file = file + 'Reserved 3\n'
	for(wpt in selectedMarkers){
		file= file + ''+ line + ',' + selectedMarkers[wpt].getTitle() + ',' + selectedMarkers[wpt].getPosition().lat().toFixed(6) + ' ,' + selectedMarkers[wpt].getPosition().lng().toFixed(7) +',41103.8137731,0, 1, 3, 0, 65535,' + selectedMarkers[wpt].getTitle() + ', 0, 0, 0, 0\n' ;
	line = line + 1;
	}
	
	var uriContent = "data:application/octet-stream," + encodeURIComponent(file);
	window.open(uriContent, 'wptFile');
}


function GenerateWPTControl(controlDiv, map) {

  controlDiv.style.paddingLeft= '5px';
  controlDiv.style.paddingTop= '50px';

  // Set CSS for the control border.
  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'LightSlateGray';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '1px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to generate the Waypoints file!';
  controlDiv.appendChild(controlUI);

  // Set CSS for the control interior.
  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'times, Times New Roman, times-roman, georgia, serif';
  controlText.style.fontSize = '17px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.style.color = 'White';
  controlText.innerHTML = '<strong>Generate WPT</strong>';
  controlUI.appendChild(controlText);

  // Setup the click event listeners: simply set the map to Chicago.
  google.maps.event.addDomListener(controlUI, 'click', function() {
    GenerateFile()
  });
}
